package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.EchartTypeEnum;
import com.example.mapper.CountChartDao;
import com.example.pojo.*;
import com.example.service.CountChartService;
import com.example.utils.SecurityUtils;
import com.example.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述
 *
 * @Author xulitao
 * @Date 2022/3/12
 * @Version 1.0
 */
@Service
public class CountChartServiceImpl implements CountChartService {

    @Autowired
    private CountChartDao dao;

    // 1-学生纬度 2-老师纬度 3-课程纬度
    @Override
    public ResultVo getScore(String type) {
        ResultVo resultVo = new ResultVo();
        if (StringUtils.isEmpty(type)) {
            resultVo.setMgs("Required field is missing");
            resultVo.setCode("500");
            return resultVo;
        }

        switch (type) {
            case "1":
                List<CountChartEntity> scoreByStu = dao.getScoreByStu();
                resultVo.setCode("200");
                resultVo.setMgs("SUCCESS");
                resultVo.setData(scoreByStu);
                return resultVo;
            case "2":
                List<CountChartEntity> scoreByTeacher = dao.getScoreByTeacher();
                resultVo.setCode("200");
                resultVo.setMgs("SUCCESS");
                resultVo.setData(scoreByTeacher);
                return resultVo;
            case "3":
                List<CountChartEntity> scoreByClass = dao.getScoreByClass();
                resultVo.setCode("200");
                resultVo.setMgs("SUCCESS");
                resultVo.setData(scoreByClass);
                return resultVo;
            default:
                resultVo.setMgs("input error");
                resultVo.setCode("406");
                return resultVo;
        }
    }

    @Override
    public Object getPlayNum(Integer type) {
        if (type.equals(1)) {
            // 饼
            List<Map<String, Object>> interactiveCountByb = dao.getInteractiveCountByb();
            return interactiveCountByb;
        } else {
            // 柱
            Map<String, Object> resultMap = new HashMap<>();
            List<String> xName = new ArrayList<>();
            List<String> yValue = new ArrayList<>();
            List<Map<String, Object>> interactiveCount = dao.getInteractiveCountByz();
            for (Map<String, Object> map : interactiveCount) {
                xName.add(map.get("xName").toString());
                yValue.add(map.get("yValue").toString());
            }
            resultMap.put("xName", xName);
            resultMap.put("yValue", yValue);
            return resultMap;
        }
    }

    @Override
    public ResultVo getWorkNum(String type) {
        ResultVo resultVo = new ResultVo();
        if (StringUtils.isEmpty(type)) {
            resultVo.setMgs("Required field is missing");
            resultVo.setCode("500");
            return resultVo;
        }

        switch (type) {
            case "1":
                List<WorkNumEntity> finishStuNum = dao.getFinishStuNum();
                resultVo.setCode("200");
                resultVo.setMgs("SUCCESS");
                resultVo.setData(finishStuNum);
                return resultVo;
            case "2":
                List<WorkNumEntity> finishTeacherNum = dao.getFinishTeacherNum();
                resultVo.setCode("200");
                resultVo.setMgs("SUCCESS");
                resultVo.setData(finishTeacherNum);
                return resultVo;
            default:
                resultVo.setMgs("input error");
                resultVo.setCode("406");
                return resultVo;
        }
    }

    @Override
    public ResultVo getScoreCount(String type) {
        ResultVo<Object> objectResultVo = new ResultVo<>();
        objectResultVo.setCode("200");
        Long currentUserId = SecurityUtils.getCurrentUserId();
        EchartTypeEnum byType = EchartTypeEnum.getByType(type);
        switch (Objects.requireNonNull(byType)) {
            case ZHUZHUANGTU:
                Map<String, Object> scoreCount = dao.getScoreCountByzz(currentUserId);
                List<BigDecimal> decimalList = dao.getScoreCountByzzAvg(currentUserId);
                if (CollectionUtil.isEmpty(decimalList)) {
                    return objectResultVo;
                }
                BigDecimal avg = decimalList.stream()
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(decimalList.size()), 2, BigDecimal.ROUND_HALF_DOWN);
                List lis1 = new ArrayList();
                List lis2 = new ArrayList();
                for (int i = 1; i <= 100; i++) {
                    lis1.add(i);
                    BigDecimal bigDecimal = new BigDecimal(i);
                    BigDecimal bigDecimal1 = new BigDecimal(i - 1);
                    long count = decimalList.stream()
                            .filter(b -> b.compareTo(bigDecimal) <= 0 && b.compareTo(bigDecimal1) > 0).count();
                    lis2.add(count);
                }
                Map hashMap = new HashMap();
                hashMap.put("name", lis1);
                hashMap.put("value", lis2);
                hashMap.put("avgScore", avg);
                objectResultVo.setData(hashMap);
                break;
            case ZHEXIANTU:
                List li1 = new ArrayList();
                List li2 = new ArrayList();
                Map<String, Object> scoreCountByzx = dao.getScoreCountByzx(currentUserId);
                scoreCountByzx.entrySet().stream()
                        .sorted(Comparator.comparingInt(o -> Integer.parseInt(o.getKey().split("~")[0])))
                        .forEach(entry -> {
                            li1.add(entry.getKey());
                            li2.add(entry.getValue());
                        });
                Map mm1 = new HashMap();
                mm1.put("name", li1);
                mm1.put("value", li2);
                objectResultVo.setData(mm1);
                break;
            case SANDIANTU:
                List list2 = new ArrayList();
                List<BigDecimal> list1 = dao.getScoreCountSdt(currentUserId);
                for (int i = 0; i < list1.size(); i++) {
                    List<BigDecimal> objects = new ArrayList<>();
                    objects.add(BigDecimal.valueOf(i));
                    objects.add(list1.get(i));
                    list2.add(objects);
                }
                objectResultVo.setData(list2);
                break;
            case LEIDATU:
                List<Map<String, Object>> map2 = dao.getScoreCountLdt(currentUserId);
                List l1 = new ArrayList();
                List l2 = new ArrayList();
                for (Map<String, Object> map : map2) {
                    Map m1 = new HashMap();
                    m1.put("name", map.get("profession"));
                    m1.put("max", 100);
                    l1.add(m1);
                    l2.add(map.get("scoreAvg"));
                }
                Map m3 = new HashMap();
                m3.put("indicator", l1);
                m3.put("data", l2);
                objectResultVo.setData(m3);
                break;
            case BINGTU:
                Map<String, Object> map = dao.getScoreCountBt(currentUserId);
                List list = new ArrayList();
                for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                    Map map1 = new HashMap();
                    map1.put("value", stringObjectEntry.getValue());
                    map1.put("name", stringObjectEntry.getKey());
                    list.add(map1);
                }
                objectResultVo.setData(list);
                break;
            default:
        }
        return objectResultVo;
    }

    @Override
    public Object getWorkScore(Integer type) {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        if (type.equals(1)) {
            // 热力图
            Map<String, Object> resultMap = new HashMap<>();
            Set<String> xName = new HashSet<>();
            List<String> yName = new ArrayList<>();
            List<JobTableEntity> jobCountByrName = dao.getJobCountByrName(currentUserId);
            if (CollectionUtil.isEmpty(jobCountByrName)) {
                return resultMap;
            }
            Map<String, List<JobTableEntity>> listMap = jobCountByrName.stream()
                    .collect(Collectors.groupingBy(i -> i.getStuName().concat("-").concat(i.getStuNumber())));
            Map<String, Long> collect = jobCountByrName.stream().collect(Collectors.groupingBy(i -> i.getStuName().concat("-").concat(i.getStuNumber()),
                    Collectors.counting()));
            Long aLong = collect.values().stream().max(Long::compareTo).orElse(0L);
            String zuoye = "作业";
            for (Long i = 1L; i <= aLong; i++) {
                yName.add(zuoye.concat(String.valueOf(i)));
            }
            List<List<Object>> resultList = new ArrayList<>();
            int i = 0;
            for (Map.Entry<String, List<JobTableEntity>> entry : listMap.entrySet()) {
                String key = entry.getKey();
                String[] split = key.split("-");
                List<JobTableEntity> value = entry.getValue();
                if (CollectionUtil.isEmpty(value)) {
                    continue;
                }
                xName.add(split[0]);
                for (int i1 = 0; i1 < value.size(); i1++) {
                    JobTableEntity jobTableEntity = value.get(i1);
                    List<Object> list = new ArrayList<>();
                    list.add(i);
                    list.add(i1);
                    list.add(jobTableEntity.getJobScore());
                    resultList.add(list);
                }
                ++i;
            }
            resultMap.put("xName", xName);
            resultMap.put("yName", yName);
            resultMap.put("value", resultList);
            return resultMap;

        } else {
            // 散点图
            List<Map<String, Object>> jobCountBys = dao.getJobCountBys();
            if (CollectionUtils.isEmpty(jobCountBys)) {
                return null;
            }
            // 出参
            List<List<Object>> paramList = new ArrayList<>();
            for (Map<String, Object> map : jobCountBys) {
                List<Object> list = new ArrayList<>();
                list.add(map.get("course"));
                list.add(map.get("value"));
                paramList.add(list);
            }
            return paramList;


        }
    }

    @Override
    public ResultVo getMaxAndMinChart() {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        List<IndexChartEntity> indexChartEntityList = dao.queryMaxAndMin(currentUserId);
        List<String> profession = indexChartEntityList.stream().map(IndexChartEntity::getProfession).collect(Collectors.toList());
        List<BigDecimal> maxScore = indexChartEntityList.stream().map(IndexChartEntity::getMaxScore).collect(Collectors.toList());
        List<BigDecimal> minScore = indexChartEntityList.stream().map(IndexChartEntity::getMinScore).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("profession", profession);
        map.put("maxScore", maxScore);
        map.put("minScore", minScore);
        ResultVo resultVo = new ResultVo();
        resultVo.setData(map);
        return resultVo;
    }
}
